﻿Imports Microforma.BEL
Imports Microforma.BLL

Public Class frmAddSolicitud

    Dim _SolicitudBE As New SolicitudBE
    Dim _SolicitudBL As New SolicitudBL
    Dim dTableIndex As DataTable

    Private _bolUpdate As Boolean
    Public Property bolUpdate() As Boolean
        Get
            Return _bolUpdate
        End Get
        Set(ByVal value As Boolean)
            _bolUpdate = value
        End Set
    End Property


    Private _IdSolicitud As Integer
    Public Property pIdSolicitud() As Integer
        Get
            Return _IdSolicitud
        End Get
        Set(ByVal value As Integer)
            _IdSolicitud = value
        End Set
    End Property

    Private _IdCliente As Integer
    Public Property pIdCliente() As Integer
        Get
            Return _IdCliente
        End Get
        Set(ByVal value As Integer)
            _IdCliente = value
        End Set
    End Property


    Private _Nombre As String
    Public Property pNombre() As String
        Get
            Return _Nombre
        End Get
        Set(ByVal value As String)
            _Nombre = value
        End Set
    End Property


    Private _IdTipo As Integer
    Public Property IdTipo() As Integer
        Get
            Return _IdTipo
        End Get
        Set(ByVal value As Integer)
            _IdTipo = value
        End Set
    End Property


    Private _IdUsuario As Integer
    Public Property IdUsuario() As Integer
        Get
            Return _IdUsuario
        End Get
        Set(ByVal value As Integer)
            _IdUsuario = value
        End Set
    End Property


    Private _Fecha As String
    Public Property Fecha() As String
        Get
            Return _Fecha
        End Get
        Set(ByVal value As String)
            _Fecha = value
        End Set
    End Property


    Private _DetalleSolicitud As String
    Public Property DetalleSolicitud() As String
        Get
            Return _DetalleSolicitud
        End Get
        Set(ByVal value As String)
            _DetalleSolicitud = value
        End Set
    End Property



    Private Sub btnBuscarCliente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscarCliente.Click
        Dim _frmClientesAdd As New frmAddListClients
        _frmClientesAdd.ShowDialog()
        pIdCliente = _frmClientesAdd.pIdCliente
        txtCliente.Text = _frmClientesAdd.pNombre
        _frmClientesAdd.Dispose()
    End Sub

    Private Sub btnAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAceptar.Click
        If ValidateForm() Then
            _SolicitudBE = setEntity()
            If Not bolUpdate Then
                If _SolicitudBL.Add(_SolicitudBE) = "OK" Then
                    Call MessageMistake("La operación se llevo a cabo con exito.", True)
                    Me.Close()
                Else
                    Call MessageMistake("Ha ocurrido problemas al llevarse a cabo la operacion. ")
                End If
            Else
                If _SolicitudBL.Edit(_SolicitudBE) = "OK" Then
                    Call MessageMistake("La operación se llevo a cabo con exito.", True)
                    Me.Close()
                Else
                    Call MessageMistake("Ha ocurrido problemas al llevarse a cabo la operacion. ")
                End If
            End If
            
        End If
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub


    Private Function ValidateForm() As Boolean
        'Pestaña General
        If Trim(txtCliente.Text) = "" Then
            Call MessageMistake("Por Favor seleccione un cliente.")
            txtCliente.Focus()
            Exit Function
        End If
        If Trim(cmbTipoSolicitud.Text) = "" Then
            Call MessageMistake("Por favor selecione un tipo de solicitud.")
            Exit Function
        End If
        If Trim(cmbResponsable.Text) = "" Then
            Call MessageMistake("Por favor selecione un responsable.")
            Exit Function
        End If

        ValidateForm = True

    End Function

    Private Function setEntity() As SolicitudBE
        Dim _SolicitudBE As New SolicitudBE
        _SolicitudBE.IdSolicitud = pIdSolicitud
        _SolicitudBE.IdCliente = pIdCliente
        _SolicitudBE.Fecha = dtpFechaSol.Text
        _SolicitudBE.IdTipo = 1 'cmbTipoSolicitud.SelectedValue
        _SolicitudBE.IdUsuario = 1 'cmbResponsable.SelectedValue
        _SolicitudBE.DetalleSolicitud = txtDetalleSolicitud.Text

        Return _SolicitudBE

    End Function

    Private Sub frmAddSolicitud_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If bolUpdate Then
            dTableIndex = _SolicitudBL.List_Id(pIdSolicitud) 'ListasBL.List(idLista)
            LoadData()
        End If

        cmbResponsable.Items.Insert(0, "[ Seleccione ]")
        cmbTipoSolicitud.Items.Insert(0, "[ Seleccione ]")

    End Sub


    Private Sub LoadData()
        For Each dtrFila As DataRow In dTableIndex.Rows
            pIdCliente = dtrFila("IdCliente")
            txtCliente.Text = dtrFila("Cliente")
            txtDetalleSolicitud.Text = dtrFila("DetalleSolicitud")
            cmbResponsable.SelectedIndex = 0
            cmbTipoSolicitud.SelectedIndex = 0
        Next

    End Sub


End Class